using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityUtilities;
/// <summary>
/// 角色技能控制脚本
/// </summary>
public class skillControl : MonoBehaviour
{    //技能列表
    public List<GameObject> skill;
    //技能触发按键绑定，和技能列表的顺序一一对应
    public List<KeyCode> keys;
    /// <summary>
    /// 技能冷却指示图片
    /// </summary>
    public List<Image> images;
    //角色属性
    public state state;
    /// <summary>
    /// 当前正在执行的技能
    /// </summary>
    private abstuctSkill currentPlayingSkill;
    private List<Countdown> cdList;
    private void Awake()
    {
        cdList = new List<Countdown>();
        for (int i = 0; i < skill.Count; i++)
        {
            cdList.Add(new Countdown(false, 0.1f));
        }
    }
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < skill.Count; i++)
        {
            //技能冷却计时器计时
            if (cdList[i].Progress())
            {
                Debug.Log("skill " + skill[i].name + " cd time up:" + cdList[i].TimePassed);
                images[i].fillAmount = 1;
            }
            else
            {
                images[i].fillAmount = cdList[i].PercentElapsed;
            }
            if (Input.GetKeyDown(keys[i]) && state.isMovable)
            {
                playSkill(i);

            }
            else
            {

            }
        }
    }
    public void playSkill(int id)
    {
        //同一时刻只允许释放一个技能,如果当前正在释放技能则不允许释放其他技能,或者技能冷却未到
        if (state.movementState != state.MovementState.PLAY_SKILL && cdList[id].ReachedZero)
        {
            var tempObject = this.skill[id] as GameObject;
            tempObject.SetActive(false);
            var skillObject = Instantiate(tempObject, this.transform.position, this.transform.rotation);
            abstuctSkill skill = skillObject.GetComponent<abstuctSkill>();
            skill.executeSkill(this.gameObject);
            currentPlayingSkill = skill;
            //重置cd倒计时
            cdList[id].Reset(skill.cooldown / 1000.0f);

        }

    }
    /// <summary>
    /// 播放技能
    /// </summary>
    /// <param name="skillPrefab">技能预制件</param>
    public void playSkill(GameObject skillPrefab)
    {
        int id = skillPrefab.GetComponent<abstuctSkill>().id;
        //同一时刻只允许释放一个技能,如果当前正在释放技能则不允许释放其他技能,或者技能冷却未到
        if (state.movementState != state.MovementState.PLAY_SKILL && cdList[id].ReachedZero)
        {
            var tempObject = skillPrefab as GameObject;
            tempObject.SetActive(false);
            var skillObject = Instantiate(tempObject, this.transform.position, this.transform.rotation);
            abstuctSkill skill = skillObject.GetComponent<abstuctSkill>();
            skill.executeSkill(this.gameObject);
            currentPlayingSkill = skill;
            //重置cd倒计时
            cdList[id].Reset(skill.cooldown / 1000.0f);

        }

    }
    /// <summary>
    /// 打断当前正在执行的技能
    /// </summary>
    public void interruptCurrentSkill()
    {
        if (currentPlayingSkill != null)
        {
            currentPlayingSkill.interruptSkill();
        }
    }

}
